# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do
  desc "Push a new beta build to fir"
  lane :tofir do
	# Deletes the Xcode Derived Data
	# clear_derived_data

 	# discarding uncommitted changes
  	# reset_git_repo(
    #   force: true,
    #   skip_clean: true,
    #   disregard_gitignore: false
    #   )


  	# git pull only the tags, no commits
	# git_pull(only_tags: true)

	# get recent tag
  	# recentTagName = last_git_tag

  	# （fastlane-plugin-git_switch_branch为插件）check out tag
  	# git_switch_branch(branch: recentTagName)

    # submodule init and update
    # git_submodule_update(
    # 	init: true,
    # 	recursive: true
    # 	)

  	# pod install
  	# cocoapods

  	scheme = "testDemos"
  	ipa_dir  = "./fastlane_build/"+Time.new.strftime('%Y-%m-%d')
    ipa_name = scheme + "_" + Time.new.strftime('%Y%m%d%H%M')
    fir_token = "fafdb1a89acf9318b61e373be0835792"

  	# gym用来编译 打包 签名
  	gym(
   	 output_directory: ipa_dir,
   	 output_name: "#{ipa_name}.ipa",
	    clean: true,
	    workspace: scheme + ".xcworkspace",
	    scheme: scheme,
	    export_xcargs: "-allowProvisioningUpdates",
	    export_options: {
	      method: "ad-hoc", # 指定打包方式
	      teamID: "N6Y5J74P46",
		  thinning: "<none>",
		  provisioningProfiles: { 
			"tomato.react-native.test" => "rn_dis",
			}
	    }
    )

  	# （fastlane-plugin-firim为插件）上传ipa到fir.im服务器，在fir.im获取firim_api_token
	  firim(firim_api_token: fir_token)
	#   upload_to_app_store(
	# 	ignore_language_directory_validation: true,
	# 	skip_metadata: true, 
	# 	skip_screenshots: true,
	# 	force: true,
	# 	username:'apple_tomato@protonmail.com' // appconnet账号
	#   )

	

	# desc "Notification 钉钉机器人"
  #   # 钉钉机器人
  # 	app_patch   = ipa_dir + "/#{ipa_name}.ipa"
  # 	app_version = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleShortVersionString")
  # 	app_build_version = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleVersion")
  # 	app_name    = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleDisplayName")
  # 	app_url 	= "xxxxxxxx"
  # 	app_icon 	= "xxxxxxx"
  # 	dingTalk_url = "xxxxxxxxx"
  
  # 	dingMessage = 
  # 	{
  #  	 msgtype: "link", 
  #  	 link: {
  #       text: "对应git tag为: #{recentTagName}", 
  #       title: "iOS #{app_name} #{app_version} (#{app_build_version}) 测试版本", 
  #       picUrl: "#{app_icon}", 
  #       messageUrl: "#{app_url}"
  #  	 	}
	# }

	# uri = URI.parse(dingTalk_url)
	# https = Net::HTTP.new(uri.host, uri.port)
	# https.use_ssl = true

	# request = Net::HTTP::Post.new(uri.request_uri)
	# request.add_field('Content-Type', 'application/json')
	# request.body = dingMessage.to_json

	# response = https.request(request)
	# puts "--------------- 钉钉机器人已经通知 ✅ ---------------"
	# puts "Response #{response.code} #{response.message}: #{response.body}"

  end
end

